package com.teyao.teyao_back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.teyao.teyao_back.entity.ResponseMessage;
import com.teyao.teyao_back.entity.User;
import com.teyao.teyao_back.service.impl.UserServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理接口")
@CrossOrigin
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    CaptureController captureController = new CaptureController();

    @GetMapping("/login")
    @Operation(summary = "登录")
    public ResponseMessage login( User user, String uuid, String code) {
        if (captureController.login(uuid, code)) {
            return userService.login(user);
        } else {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setMsg("验证码错误");
            return responseMessage;
        }
    }

    @PostMapping("/register")
    @Operation(summary = "注册")
    public ResponseMessage register(User user) {
        System.out.println(user);
        return userService.register(user);
    }


    //增
    @PostMapping("/add")
    public Boolean save(User user) {
        return userService.save(user);
    }

    //删
    @GetMapping("/delete")
    public ResponseMessage delete(Integer userId) {
        return userService.deleteList(userId);
    }

    //改
    @PostMapping("/mod")
    public ResponseMessage mod(User user, Integer userId) {
        return userService.updateList(userId, user);
    }

    //查（模糊查询）
    @PostMapping("/listP")
    public List<User> listP(User user) {
        LambdaQueryWrapper<User> LambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper.like(User::getUsername, user.getUsername());
        return userService.list(LambdaQueryWrapper);
    }
}
